首页 > 试题广场 >

字符统计

[编程题]字符统计
  • 热度指数:201846 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
输入一个只包含小写英文字母和数字的字符串,按照不同字符统计个数由多到少输出统计结果,如果统计的个数相同,则按照ASCII码由小到大排序输出。

数据范围:字符串长度满足


输入描述:

一个只包含小写英文字母和数字的字符串。



输出描述:

一个字符串,为不同字母出现次数的降序表示。若出现次数相同,则按ASCII码的升序输出。

示例1

输入

aaddccdc

输出

cda

说明

样例里,c和d出现3次,a出现2次,但c的ASCII码比d小,所以先输出c,再输出d,最后输出a.
     
input_str = input()
statistics = {}
for char in input_str:
    if char not in statistics:
        statistics[char] = 1
    else:
        statistics[char] += 1

sorted_char = sorted(statistics.items(), key=lambda item: (-item[1], ord(item[0])))

output = ""
for i in range(len(sorted_char)):
    output += sorted_char[i][0]
 
print(output)

发表于 2023-09-27 17:42:13 回复(0)
shuru = str(input())
from collections import Counter
a = Counter(sorted(shuru))
b = a.most_common()
element = []
for item in b:
    element.append(item[0])

print(''.join(element))
发表于 2023-08-12 14:30:48 回复(0)
inp=input()
s=sorted(inp)
s=list(set(s))
s=sorted(s)
dic={}
for i in s:
    dic[i]=inp.count(i)

list_v=sorted(dic.values(),reverse=True)

res=''
for k in list_v:
    for key,value in dic.items():
        if value==k and key not in res:
            res+=key
            break
print(res)

发表于 2023-07-25 21:08:57 回复(0)
n = input()
l={}
for i in n:
    l[i]=n.count(i)

while (l != {}):
    max_list = []
    max_value = max(l.values())
    for m, n in l.items():
        if n == max_value:
            max_list.append(m)
    max_list = sorted(max_list)
    for i in max_list:
        print(i, end='')
    for i in max_list:
        del l[i]








发表于 2022-09-07 22:58:52 回复(0)
a = input()
dic = {}
over = []
result = ""
for i in a:
    if i not in over:
        over.append(i)
        if a.count(i) not in dic:
            dic[a.count(i)] = [i]
        else:
            dic[a.count(i)].append(i)
            
keys = list(dic.keys())
keys.sort()
keys.reverse()
for i in keys:
    dic[i].sort()
    for m in dic[i]:
        result += m
print(result)

发表于 2022-08-27 01:39:13 回复(0)
冒泡排序
s=input()
s_new=''
for c in s:
    if c not in s_new:
        s_new=s_new+c
s_new=list(s_new)
n=len(s_new)
for i in range(n):
    for j in range(n-i-1):
        if s.count(s_new[j])<s.count(s_new[j+1]):
            s_new[j],s_new[j+1]=s_new[j+1],s_new[j]
        elif s.count(s_new[j])==s.count(s_new[j+1]):
            if s_new[j]>s_new[j+1]:
                s_new[j],s_new[j+1]=s_new[j+1],s_new[j]

for c in s_new:
    print(c,end='')


发表于 2022-08-16 10:09:37 回复(0)
s=input()
d={}
for i in s:
    if i in d.keys():
        d[i]+=1
    else:
        d[i]=1
l=sorted(d.items(),key=lambda x :x[0])
l=sorted(l,key=lambda x :x[1],reverse=True)
for k,v in l:
    print(k,end='')
发表于 2022-08-08 23:27:41 回复(0)
s = input()
# 输入的字符串去重并根据ASC11升序排序
list1 = sorted(set(s))
# print(list1)
# 对list1中的字母按照该字符在输入中出现的次数进行降序排序,
#由于list1已经对字符进行升序排序了,所以数字相同时自动会按照升序排序
list2 = sorted(list1,key=lambda x:s.count(x),reverse=True)
# print(list2)
print(''.join(list2))

发表于 2022-07-24 15:14:17 回复(0)
inp = input()
new_inp = list(set(inp))
lst = []
for i in new_inp:
    short_lst = []
    short_lst.append(i)
    short_lst.append(inp.count(i))
    lst.append(short_lst)
lst.sort(key=lambda x:(-x[1], x[0]))
for i in lst:
    print(i[0], end='')

发表于 2022-07-19 03:21:27 回复(0)
data = list(input())
# print(data)   #['a', 'a', 'd', 'd', 'c', 'c', 'd', 'c']
# data.sort(key=ord)
dic = {}
out = []
for i in data:
    if i not in dic:
        dic[i] = 1
        out.append(i)
    else:
        dic[i] += 1
for i in range(len(out)-1):
    for j in range(i,len(out)):
        if dic[out[i]] < dic[out[j]]:
            out[i],out[j] = out[j],out[i]
        elif dic[out[i]] == dic[out[j]]:
            if ord(out[i]) > ord(out[j]):
                out[i],out[j] = out[j],out[i]
for i in out:
    print(i,end="")
#     输入6uym66c0l609vb6mg75q90zyf9d4styi257709
#     6097y5m24bcdfgilqstuvz
发表于 2022-07-18 18:24:35 回复(0)
n = input()
count = {}

for i in n:
    if i not in count:
        count[i] = 1
    else:
        count[i]+=1
        
for k, v in sorted(count.items(), key=lambda x:(-x[1], x[0])):
    print(k, end='')

发表于 2022-07-13 20:53:39 回复(0)
while True:
    try:
        words = input()
        new_words = []
        target = []
        for word in set(words):
            new_words.append((word, words.count(word)))
        new_words.sort(key=lambda x: (-x[1], x[0]))
        for letter in new_words:
            target.append(letter[0])
        print(''.join(target))
    except:
        break
发表于 2022-06-25 13:21:12 回复(0)
while True:
    try:
        s=input()
        d={}
        for i in str(s):
            if i in d.keys():
                d[i]+=1
            elif i not in d.keys():
                d[i]=1
        for key, value in sorted(sorted(d.items()),key=lambda x:x[1], reverse=True):
            print(key,end='')
    except:
        break

发表于 2022-06-17 16:08:13 回复(0)
str1 =input()
dic = {}
str2=''
for i in str1:
    dic[i] = str1.count(i)
for t in sorted(dic,key=lambda x:(dic[x],-ord(x)),reverse=True):
    str2=str2+t
print(str2)
发表于 2022-06-10 11:42:25 回复(0)
from collections import defaultdict
text = input()
dic = defaultdict(int)
for ch in text:
    dic[ch]+=1
for key,value in sorted(dic.items(),key=lambda x:x[1]*1000 - ord(x[0]),reverse=True):
    print(key,end="")

发表于 2022-06-06 21:10:34 回复(0)
s = input()
ns = sorted(sorted(set(s)),key=lambda x:s.count(x) ,reverse=True)
print(''.join(ns))
sort()默认提供按ASCII码排序,再按出现次数倒序排序
所以两次使用sort
发表于 2022-06-04 19:21:43 回复(0)
a=input()
dic1={}
for i in a:
    if i not in dic1.keys():
        dic1[i]=1
    else :
        dic1[i]=dic1[i]+1
orcdic=sorted(dic1.items(),key = lambda x:ord(x[0]))
orcdic1= sorted(orcdic,key = lambda x:x[1],reverse=True)
orcdic2=dict(orcdic1)
print(''.join(orcdic2.keys()))

    

发表于 2022-05-29 22:08:32 回复(0)
while True:
    try:
        s=input()
        d=dict()
        for ch in s:
            if d.get(ch):
                d[ch]=d[ch]+1
            else:
                d[ch]=1
        a1 = sorted(d.items(), key=lambda x: (-x[1],x[0]), reverse=False)
        for i in a1:
            print(i[0],end='')
    except:
        break

发表于 2022-05-25 13:25:18 回复(0)
while True: try:
        str1 = input() if str1.isalnum() and 1 <= len(str1) <= 1000:
            arr1 = list(set(list(str1))) # 新建字典,读取到每个字符在字符串中的数量  dic = {} for x in arr1:
                dic[x] = str1.count(x) # 新建数组,存放每个字符的数量,去重后倒序排列  arr2 = [] for y in dic.values():
                arr2.append(y)
            arr2 = list(set(arr2))
            arr2.sort(reverse=True) # 新建字典,将数量作为keys,多个字母作为字符串放入value里面  dic2 = {} for n in arr2:
                str2 = ""  for m in dic.keys(): if dic.get(m) == n:
                        str2 = str2 + m
                dic2[n] = str2 # 打印排序结果,如果value长度大于1,需要进行ASCII码排序  for val in dic2.values(): if len(val) == 1: print(val, end="") elif len(val) > 1:
                    arr3 = list(map(ord, list(val)))
                    arr3.sort()
                    arr3 = list(map(chr, arr3)) for v in arr3: print(v, end="") break  except: break
发表于 2022-05-04 11:28:26 回复(0)